package com.pwt.controller.watch;

import com.pwt.common.CustomException;
import com.pwt.common.ResultPage;
import com.pwt.model.*;
import com.pwt.model.what.CharacteristicsModel;
import com.pwt.model.what.OverviewModel;
import com.pwt.model.what.WhatDetailsModel;
import com.pwt.service.CurvesService;
import com.pwt.service.WhatDetailsService;
import com.pwt.utils.WordUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author dw
 * @version 1.0.0
 * @date 2021/9/23
 */
@RestController
@Tag(name = "监控详情查询")
public class WhatDetailsController {

    private WhatDetailsService whatDetailsService;

    private CurvesService curvesService;

    private WordUtils wordUtils;

    public WhatDetailsController(WhatDetailsService whatDetailsService, CurvesService curvesService, WordUtils wordUtils) {
        this.whatDetailsService = whatDetailsService;
        this.curvesService = curvesService;
        this.wordUtils = wordUtils;
    }

    @GetMapping("/measured")
    @Operation(description = "测试值包括历史",summary = "测试值包括历史")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号"),
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "展示数",required = true)
    })
    public WhatDetailsModel<CharacteristicsModel> getData(@RequestParam String k0014,@RequestParam Integer k0010,@RequestParam Integer page,@RequestParam Integer limit){
        return this.whatDetailsService.getData( k0014,  k0010,  page,  limit);
    }

    @GetMapping("/measured/value-chart")
    @Operation(description = "测试值包括历史 根据被测参数 折线图",summary = "测试值包括历史 根据被测参数 折线图 ")
    @Parameters({
            @Parameter(name = "k1000",description = "零件编号"),
            @Parameter(name = "k2000",description = "被测参数号"),
            @Parameter(name = "k0010",description = "机器编号"),
            @Parameter(name = "k0014",description = "零件id")
    })
    public ChartModel getListsAllValues(@RequestParam Integer k1000,@RequestParam Short k2000,@RequestParam Integer k0010,@RequestParam String k0014) throws CustomException {
        return this.whatDetailsService.getListsAllValues(k1000,k2000,k0010,k0014,40);
    }

    @GetMapping("/overview")
    @Operation(description = "特性概览+数值图表",summary = "特性概览+数值图表")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号"),
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "展示数",required = true)
    })
    public WhatDetailsModel<OverviewModel> getOverViewValueChart(@RequestParam String k0014, @RequestParam Integer k0010, @RequestParam Integer page, @RequestParam Integer limit){
        return this.whatDetailsService.getOverViewValueChart( k0014,  k0010,  page,  limit);
    }


    @GetMapping("/overview/value-chart")
    @Operation(description = "特性概览+数值图表 折线图   ",summary = "特性概览+数值图表 折线图")
    @Parameters({
            @Parameter(name = "k1000",description = "零件编号"),
            @Parameter(name = "k2000",description = "被测参数号"),
            @Parameter(name = "k0010",description = "机器编号"),
            @Parameter(name = "k0014",description = "零件id")
    })
    //@Parameter(name = "k0006",description = "批次号"),
    public ChartModel getOverviewLists(@RequestParam Integer k1000, @RequestParam Short k2000, @RequestParam Integer k0010, @RequestParam String k0014) throws CustomException {
        return this.whatDetailsService.getListsAllValues(k1000,k2000,k0010,k0014,125);
    }

    @GetMapping("/xbar-r")
    @Operation(description = " 极差图   ",summary = "极差图")
    @Parameters({
            @Parameter(name = "k1000",description = "零件编号"),
            @Parameter(name = "k2000",description = "被测参数号"),
            @Parameter(name = "k0010",description = "机器编号"),
            @Parameter(name = "k0014",description = "零件id")
    })
    public XbarRModel XBarAndRBar(@RequestParam Integer k1000, @RequestParam Short k2000, @RequestParam Integer k0010, @RequestParam String k0014) throws CustomException {
        return this.whatDetailsService.XBarAndRBar(k1000,k2000,k0010,k0014);
    }

    @GetMapping("/has-curves")
    @Operation(description = "菜单判断是否有曲线数据",summary = "菜单判断是否有曲线数据")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号")
    })
    public Integer hasCurves(@RequestParam String k0014, @RequestParam Integer k0010){
        return this.curvesService.hasCurves(k0014,k0010);
    }

    @GetMapping("/curves-history")
    @Operation(description = "曲线历史集合",summary = "曲线历史集合")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号"),
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "展示数",required = true)
    })
    public ResultPage<CurvesHistoryModel> curvesHistory(@RequestParam String k0014, @RequestParam Integer k0010, Integer page, Integer limit){
        return this.curvesService.queryHistoryCurves(k0014,k0010,page,limit);
    }

    @GetMapping("/curves-historyTree")
    @Operation(description = "曲线历史集合树形结构",summary = "曲线历史集合树形结构")
    @Parameters({
            @Parameter(name = "k0010",description = "机器号",required = true),
            @Parameter(name = "beginTime",description = "开始时间 yyyy-MM-dd HH:mm:ss",required = true),
            @Parameter(name = "endTime",description = "结束时间 yyyy-MM-dd HH:mm:ss",required = true)
    })
    public List<TreeComBoxModel> curvesHistoryTree(@RequestParam(required = true) Integer k0010,@RequestParam(required = true) String beginTime,@RequestParam(required = true) String endTime){
        return this.curvesService.queryTreeList(beginTime,endTime,k0010);
    }

    @GetMapping("/curves-table")
    @Operation(description = "时间力矩位移页面 table表头接口",summary = "时间力矩位移页面 table表头接口")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号"),
            @Parameter(name = "page",description = "页码",required = true),
            @Parameter(name = "limit",description = "展示数",required = true)
    })
    public WhatDetailsModel<CharacteristicsModel> curvesOneTable(@RequestParam String k0014, @RequestParam Integer k0010, @RequestParam Integer page, @RequestParam Integer limit){
        return this.curvesService.curvesOneTable(k0014,k0010,page,limit);
    }

    @GetMapping("/curves-time")
    @Operation(description = "stroke-force-time",summary = "时间 曲线接口")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号")
    })
    public List<CurvesModel> curvesOne(@RequestParam String k0014, @RequestParam Integer k0010){
        return this.curvesService.curvesOne(k0014,k0010);
    }


    @GetMapping("/curves-force-stroke")
    @Operation(description = "stroke-force-stroke",summary = "力矩位移接口")
    @Parameters({
            @Parameter(name = "k0014",description = "零件id"),
            @Parameter(name = "k0010",description = "机器号")
    })
    public CurvesModel curvesForceAndStroke(@RequestParam String k0014, @RequestParam Integer k0010){
        return this.curvesService.curvesForceAndStroke(k0014,k0010);
    }

    @PostMapping("/curves-time32")
    @Operation(description = "curves-times",summary = "时间 曲线接口集合")
    public List<ResultCurvesModel> curves32(@RequestBody(required = true) CurvesParameterModel curves) throws CustomException {
        return this.curvesService.curvesList(curves.getK0014s(),curves.getK0010());
    }


    @PostMapping("/curves-force-stroke32")
    @Operation(description = "stroke-force-strokes",summary = "力矩位移接口集合")
    public List<CurvesModel> curvesForceAndStroke32(@RequestBody(required = true) CurvesParameterModel curves) throws CustomException {
        return this.curvesService.curvesForceAndStrokeList(curves.getK0014s(),curves.getK0010());
    }

    @GetMapping("/exportChart")
    @Operation(description = "exportChart",summary = "导出word")
    @Parameters({
            @Parameter(name = "k1000",description = "零件编号"),
            @Parameter(name = "k2000",description = "被测参数编号"),
            @Parameter(name = "beginTime",description = "开始时间"),
            @Parameter(name = "endTime",description = "结束时间")
    })
    public void curvesForceAndStroke(@RequestParam Integer k1000, @RequestParam Short k2000, String beginTime, String endTime, HttpServletResponse response) throws IOException {
         Map map = this.whatDetailsService.exportChart(k1000,k2000,beginTime,endTime);
         this.wordUtils.exportWord(null,response,map,"测试word导出","wordftl.ftl");
    }


}
